using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Decav.Gatsb.Data;
using Decav.Gatsb.Application;
using Decav.Gatsb;

public partial class LocationTileControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    private void InitializeTile()
    {
        if (Location == null)
            return;

        currentLocationLi.Visible = Page.User.Identity.IsAuthenticated;

        locationNameLink.HRef = viewProfileHref.HRef = string.Format("Place.aspx?id={0}", LocationId);
        locationNameLink.InnerText = Location.ToString();
        addressLiteral.Text = Location.Address.Address1;

        LocationCheckin checkin = null;
        if (HttpContext.Current.User.Identity.IsAuthenticated)
            checkin = GatsbApplication.Current.GetUserLocation(GatsbApplication.Current.CurrentUser);

        if (checkin != null && checkin.LocationSpecified && checkin.Location.Id == Location.Id)
        {
            currentLocationLiteral.Visible = true;
            setLocationLink.Visible = false;

            currentLocationLiteral.Text = "You are currently here.";
        }
        else
        {
            currentLocationLiteral.Visible = false;
            setLocationLink.Visible = true;
        }
    }


    private Decav.Gatsb.Location _location;

    /// <summary>
    /// Gets or sets the user the tile control displays.
    /// </summary>
    public Decav.Gatsb.Location Location
    {
        get
        {
            if (_location == null && LocationId != Guid.Empty)
                RefreshLocation();

            return _location;
        }
        set
        {
            _location = value;

            if (_location != null)
                LocationId = _location.Id;
            else
                LocationId = Guid.Empty;

            if (value != null)
                InitializeTile();
        }
    }

    /// <summary>
    /// Gets the ID of the location (Saved in viewstate);
    /// </summary>
    public Guid LocationId
    {
        get
        {
            if (ViewState["LocationId"] == null)
                return Guid.Empty;

            return (Guid)ViewState["LocationId"];
        }
        private set
        {
            ViewState["LocationId"] = value;

            if (_location == null || _location.Id != value)
                RefreshLocation();
        }
    }

    private void RefreshLocation()
    {
        if (LocationId == Guid.Empty)
            _location = null;

        using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
            _location = adapt.GetLocation(LocationId);
    }

    protected void SetLocationLink_Click(object sender, EventArgs e)
    {
        GatsbApplication.Current.SetUserLocation(LocationId);
        InitializeTile();
    }
}
